package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> getCategory(CategoryPageQueryDTO dto) {
        PageResult pageResult = categoryService.getCategory(dto);
        return Result.success(pageResult);
    }

    /**
     * 修改分类
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result<Category> updataCategory(@RequestBody CategoryDTO dto) {
        Category category = categoryService.updataCategory(dto);
        return Result.success(category);
    }

    /**
     * 启用禁用
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("启用禁用")
    public Result status(@PathVariable Integer status, Integer id) {
        categoryService.status(status, id);
        return Result.success();
    }

    /**
     * 增加分类
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("增加分类")
    public Result saveCategory(@RequestBody CategoryDTO dto) {
        categoryService.saveCategory(dto);
        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteById(Integer id) {

        categoryService.deleteById(id);
        return Result.success();

    }

    /**
     * 根据类型查询分类
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> getCategoryList(Integer type) {
        List<Category> categorylist = categoryService.getCategoryList(type);
        return Result.success(categorylist);
    }
}
